package tongwentongshu.com.app.activity;

import android.os.CountDownTimer;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.contract.BackPasswordContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.presenter.BackPasswordPresenter;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.SpUtils;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/13.
 */

public class RebuildPasswordActivity extends BaseActivity {
    @BindView(R.id.et_login_old_password)
    EditText oldPw;
    @BindView(R.id.et_login_password)
    EditText etPw;
    @BindView(R.id.et_login_again_password)
    EditText etRpw;
    private Map<String, String> map;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_password_rebuild;
    }

    @Override
    protected int getTitleId() {
        return R.string.rebuild_password;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick({ R.id.btn_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_complete:
                modifyPwd();
                break;
        }
    }

    void modifyPwd() {
        String opw = oldPw.getText().toString().trim();
        final String pw = etPw.getText().toString().trim();
        String rpw = etRpw.getText().toString().trim();
        if (TextUtils.isEmpty(opw)) {
            ToastUtil.show(mContext, "请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(pw)) {
            ToastUtil.show(mContext, "请设置密码");
            return;
        }
        if (TextUtils.isEmpty(rpw)) {
            ToastUtil.show(mContext, "请再次输入密码");
            return;
        }
        if (!rpw.equals(pw)) {
            ToastUtil.show(mContext, "两次密码不一致");
            return;
        }
        map = new ArrayMap<>();
        map.put("newpwd", pw);
        map.put("rawpwd", opw);
        map.put("token", Cache.getToken());
        UpdateFractory.getBuild()
                .name("ModifyPwdCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem poiBean, String msg) {
                        ToastUtil.show(mContext, msg);
                        SpUtils.setString(mContext, Cache.USER_PASSWORD, pw);
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

}
